package us.curino.sql2cat.client;

import java.util.Random;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.MultiWordSuggestOracle;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SuggestBox;
import com.google.gwt.user.client.ui.TextArea;
import us.curino.sql2cat.client.generateCategoryGraph.Util;
import com.gwtext.client.widgets.Panel;
import com.google.gwt.user.client.ui.HTMLPanel;

public class S2C implements EntryPoint {

	public void onModuleLoad() {
		RootPanel rootPanel = RootPanel.get();
		
	    final String myhost = "http://vise4.csail.mit.edu/cat/";
	    
		MultiWordSuggestOracle oracle = new MultiWordSuggestOracle();  
		oracle.add("SELECT employee.ename, employee.hiredate FROM employee WHERE employee.hiredate > \"2001\";");
		oracle.add("SELECT department.dname, employee.ename AS depManager FROM department, employee WHERE employee.deptno = department.did AND department.did = \"101\";");
	    oracle.add("SELECT employee.ename, department.manager FROM employee, department WHERE employee.deptno = department.did AND employee.id=\"7\";");		
	    
		final Image image_3 = new Image(myhost+"right.gif");
		rootPanel.add(image_3, 421, 560);
		image_3.setSize("93px", "148px");
		image_3.setVisible(false);
		final Image image_4 = new Image(myhost+"left.gif");
		rootPanel.add(image_4, 1023, 558);
		image_4.setSize("93px", "148px");
		image_4.setVisible(false);
		
		final HTML functForw = new HTML("F:", true);
		rootPanel.add(functForw, 431, 685);
		functForw.setSize("63px", "25px");
		functForw.setVisible(false);
		
		final HTML functorBack = new HTML("G:", true);
		rootPanel.add(functorBack, 1033, 683);
		functorBack.setSize("93px", "190px");
		functorBack.setVisible(false);
		
		HTML splitline = new HTML("<hr>", true);
		rootPanel.add(splitline, 40, 401);
		
		Panel pnlInputSchemasql = new Panel("Input Schema (SQL DDL) ");
		
		final TextArea textArea = new TextArea();
		pnlInputSchemasql.add(textArea);
		textArea.setText("CREATE TABLE employee(\n"+
						 " id int,\n"+
						 " ename varchar,\n"+
						 " hiredate date,\n"+
						 " deptno int,\n"+
						 " PRIMARY KEY (id),\n"+
						 " FOREIGN KEY fk1 (deptno) REFERENCES  department(did)\n"+
						 ");\n\n"+
							
						 "CREATE TABLE department(\n"+
							" did int,\n"+
							" dname varchar,\n"+
							" manager int,\n"+
							" PRIMARY KEY (did),\n"+
							" FOREIGN KEY fk2 (manager) REFERENCES employee(id)\n"+
							" );\n");

		textArea.setSize("356px", "300px");
		rootPanel.add(pnlInputSchemasql, 24, 70);
		pnlInputSchemasql.setSize("366px", "337px");
		
		Panel panel = new Panel("Categorical Schema");
		
		final Image schemaImage2 = new Image((String)null);
		panel.add(schemaImage2);
		schemaImage2.setSize("366px", "278px");
		
		final HTML schema2 = new HTML("INPUT SCHEMA", true);
		panel.add(schema2);
		schema2.setSize("186px", "190px");
		rootPanel.add(panel, 24, 480);
		panel.setSize("366px", "479px");
		
		final Panel pnlCategoryOfQuery = new Panel("Category of Query Selection");
		
		pnlCategoryOfQuery.setVisible(false);
		
		final Image image = new Image((String) null);
		pnlCategoryOfQuery.add(image);
		image.setSize("467px", "314px");
		image.setVisible(false);
		
		final HTML querySelect = new HTML("Query Select", true);
		pnlCategoryOfQuery.add(querySelect);
		querySelect.setSize("357px", "101px");
		rootPanel.add(pnlCategoryOfQuery, 520, 480);
		pnlCategoryOfQuery.setSize("467px", "479px");
		
		final Panel pnlCategoryOfQuery_1 = new Panel("Category of Query Projection");

		pnlCategoryOfQuery_1.setVisible(false);
		
		final Image queryProjImage = new Image((String) null);
		pnlCategoryOfQuery_1.add(queryProjImage);
		queryProjImage.setSize("186px", "278px");
		
		final HTML queryProj = new HTML("Query Projection", true);
		pnlCategoryOfQuery_1.add(queryProj);
		queryProj.setSize("186px", "93px");
		rootPanel.add(pnlCategoryOfQuery_1, 1149, 480);
		pnlCategoryOfQuery_1.setSize("196px", "479px");
		
		final Panel pnlInputQuerysql = new Panel("Input Query (SQL)");
		
		pnlInputQuerysql.setVisible(false);
		
		final SuggestBox textBox = new SuggestBox(oracle);
		pnlInputQuerysql.add(textBox);
		textBox.setSize("460px", "13px");
			
		
		Button btnUpdateschema = new Button("UpdateSchema");
		rootPanel.add(btnUpdateschema, 24, 416);
		btnUpdateschema.setSize("366px", "24px");
		btnUpdateschema.setText("Generate Categorical Schema");
		
		rootPanel.add(pnlInputQuerysql, 514, 334);
		pnlInputQuerysql.setSize("473px", "51px");
		
		final Image querySelectImage = new Image((String) null);
		pnlInputQuerysql.add(querySelectImage);
		querySelectImage.setSize("468px", "278px");
		
		final Button button = new Button("New button");
		rootPanel.add(button, 514, 416);
		button.setSize("473px", "24px");
		button.setText("Generate Categorical Query");
		
		HTML htmldatabaseToCategory = new HTML("<h1>Database to Category Theory Translator </h1> ", true);
		rootPanel.add(htmldatabaseToCategory, 365, 10);
		htmldatabaseToCategory.setSize("763px", "33px");
		
		button.setVisible(false);
		
		functForw.setVisible(false);
		functorBack.setVisible(false);
		
		
		
		button.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				generateCategoryGraphAsync t = Util.getInstance();
				
				AsyncCallback callback = new AsyncCallback() {

					@SuppressWarnings("unchecked")
					public void onSuccess(Object outcome) {
						
						schema2.setHTML(((CommuteContainer)outcome).schema.toString());
						querySelect.setHTML(((CommuteContainer)outcome).sel.toString());
						

						functForw.setVisible(true);
						functorBack.setVisible(true);
						
						functForw.setHTML("F: (obvious inclusion,\n i.e., object and arrow\n labels are preserved)");
						functorBack.setHTML("G: (object labels are preserved)\n" + ((CommuteContainer)outcome).functPull.toString());
						queryProj.setHTML(((CommuteContainer)outcome).proj.toString());
						
						
						
						Random r = new Random();
						//html.setHTML(outcome.toString());
						schemaImage2.setUrl(myhost+"schema.gif?variable="+(r.nextInt()));
						queryProjImage.setUrl(myhost+"queryProject.gif?variable="+(r.nextInt()));
						querySelectImage.setUrl(myhost+"querySelect.gif?variable="+(r.nextInt()));
						image.setUrl(myhost+"querySelect.gif?variable="+(r.nextInt()));
						image_3.setVisible(true);
						image_4.setVisible(true);
						image.setVisible(true);


						pnlCategoryOfQuery.setVisible(true);
						pnlCategoryOfQuery_1.setVisible(true);
						pnlInputQuerysql.setVisible(true);
						
					}

					public void onFailure(Throwable caught) {
						System.out.println("ERROR");
					}
				};
				t.sendSQL(textBox.getText(), callback);
			}
		});
	
	
		

	
	
	btnUpdateschema.addClickHandler(new ClickHandler() {
		public void onClick(ClickEvent event) {
			generateCategoryGraphAsync t = Util.getInstance();
			
			AsyncCallback callback = new AsyncCallback() {

				@SuppressWarnings("unchecked")
				public void onSuccess(Object outcome) {
					
					schema2.setHTML(((CommuteContainer)outcome).schema.toString());
					
					Random r = new Random();
					//html.setHTML(outcome.toString());
					schemaImage2.setUrl(myhost+"schema.gif?variable="+(r.nextInt()));
					queryProjImage.setUrl((String) null);
					querySelectImage.setUrl((String) null);
					image.setUrl((String) null);
				
					image_3.setVisible(false);
					image_4.setVisible(false);

					image.setVisible(false);

					
					functForw.setVisible(false);
					functorBack.setVisible(false);
					
					
					pnlCategoryOfQuery.setVisible(false);
					pnlCategoryOfQuery_1.setVisible(false);
					pnlInputQuerysql.setVisible(true);

					button.setVisible(true);
					
					
				}

				public void onFailure(Throwable caught) {
					System.out.println("ERROR");
				}
			};
			t.sendSchemaSQL(textArea.getText(), callback);
		}
	});
	}	
}
